let ticketAry = [];
let ticketIndexAry = [];
let generateSeats = function(seatAry,tickets){
    let seatsTableStr = "";
    let rowAry = [];
    //将数据库中已选择座位设置到座位的数组中
    for(let i = 0;tickets && i < tickets.length ;i++){
        let indexes = tickets[i].split("_");
        seatAry[indexes[1]][indexes[2]] = 2;
    };
    for(let i = 0,r = 1;i < seatAry.length;i++){
        let temp = "";
        let isRowNull = true;
        for(let j = 0,c = 1;j < seatAry[i].length;j++){

            if(seatAry[i][j] == 0){
                temp += "<i class='seat_null' id='seat_"+i+"_"+j+"' >&nbsp;</i>";
            }else if(seatAry[i][j] == 1){
                temp += "<i class='seat_optional' id='seat_"+i+"_"+j+"' colname='"+r+"排"+c+"座' ></i>";
                isRowNull = false;
                c++;
            }else if(seatAry[i][j] == 2){
                temp += "<i class='seat_noselected' id='seat_"+i+"_"+j+"' colname='"+r+"排"+c+"座' ></i>";
                isRowNull = false;
                c++;
            }

        }
        if(!isRowNull){
            rowAry.push(r);
            r++;
        }else{
            rowAry.push(-1);
        }
        seatsTableStr += "<tr><td>"+temp+"</td></tr>"
    }
    seatsTableStr = "<table class='dynamicSeats'>"+seatsTableStr+"</table>"
    $(".seats").html(seatsTableStr);

    //为每个座位生成事件
    $(".seat_optional").click(function(){

        if($(this).attr("class") == "seat_optional"){
            if(ticketAry.length >= 4){
                alert("不能买超过4张");
                return;
            }
            ticketAry.push($(this).attr("colname"));
            ticketIndexAry.push($(this).attr("id"));
            $(this).attr("class","seat_selected");
        }else if($(this).attr("class") == "seat_selected"){
            ticketAry.splice(ticketAry.indexOf($(this).attr("colname")),1);
            ticketIndexAry.splice(ticketIndexAry.indexOf($(this).attr("id")),1);
            $(this).attr("class","seat_optional");
        }
        let ticketStr = "";
        $(ticketAry).each(function(){
            ticketStr += "<span class='ticket'>"+this+",</span>"
        });
        $(".ticket").remove();
        $(".tickets").append(ticketStr);
    });

    //生成排号
    let rowTableStr = "";
    for(let i = 0;i < rowAry.length;i++){
        if(rowAry[i] == -1){
            rowTableStr += "<tr><td>&nbsp;</td></tr>";
        }else{
            rowTableStr += "<tr><td>"+rowAry[i]+"排</td></tr>";
        }

    }
    rowTableStr = "<table class='rowTable'>"+rowTableStr+"</table>";
    $(".seats").append(rowTableStr);

}
export {
    ticketIndexAry,
    generateSeats
}
